什么是IoC?
反转控制,相对于原始的控制流相对复杂一些。 反转控制是流程反转,更多的组件创建的流程更加透明。 不需要过多的关注细节 依赖查找,依赖注入
IoC发展简介
- IoC 的简史
- 1983年,Richard E. Sweet 在《The Mesa Programming Environment》中提出“Hollywood Principle”(好莱坞原则)
- 1988年,Ralph E. Johnson & Brian Foote 在《Designing Reusable Classes》中提出“Inversion of control”(控制反转)
- 1996年,Michael Mattsson 在《Object-Oriented Frameworks, A survey of methodological issues》中将“Inversion of control”命名为 “Hollywood principle”
- 2004年,Martin Fowler 在《Inversion of Control Containers and the Dependency Injection pattern》中提出了自己对 IoC 以及 DI 的理解
- 2005年,Martin Fowler 在 《InversionOfControl》对 IoC 做出进一步的说明
IoC主要实现策略
- Using a service locator pattern使用服务定位器模式
- Using dependency injection, for example依赖注入
- Constructor injection 构造器注入
- Parameter injection 参数注入
- Setter injection setter
- Interface injection 接口注入
- Using a contextualized lookup 上下文
- Using template method design pattern 模版方式
- Using strategy design pattern 策略设计模式
IoC容器的职责
- 通用职责
- 依赖处理
- 依赖查找
- 依赖注入
- 生命周期管理
- 容器
- 托管的资源(Java Beans或者其他资源)
- 配置
- 容器
- 外部化配置
- 托管的资源(Java Beans或者其他资源)
IoC容器的实现
- 主要实现
- Java SE
- Java Beans
- Java ServiceLoader SPI
- JNDI (Java Naming and Directory Interface)
- Java EE
- EJB(Enterprise Java Beans) 3.0之后依赖查找,依赖注入并存
- Servlet
- 开源
- Apache Avalon(http://avalon.apache.org/closed.html)
- PicoContainer(http://picocontainer.com/)
- Google Guice(https://github.com/google/guice)
- Spring Framework(https://spring.io/projects/spring-framework)